import { useEmployeeStore } from '@/stores/employee.store.ts'
import { storeToRefs } from 'pinia'
import type { EmployeeInfo, Page } from '@/hooks/employee.hook.type.ts'
import { toRefs, unref } from 'vue'
import { DialogType, type SearchForm } from '@/stores/employee.store.type.ts'
import type { Result } from '@/utils/http.ts'
import http from '@/utils/http.ts'

export const useEmployeeSearch = () => {
  const store = useEmployeeStore()
  const { search } = storeToRefs(store)
  const { form } = toRefs(search.value)

  function clearSearchForm() {
    form.value = {} as SearchForm
  }

  return {
    clearSearchForm
  }
}

export const useEmployeeTable = () => {
  const { table } = storeToRefs(useEmployeeStore())
  const {
    height,
    list,
    total,
    $name,
    $location,
    $pageSize,
    $currentPage,
    currentEmployee
  } = toRefs(table.value)

  async function fetchDataForList() {
    const params = {
      name: unref($name),
      location: unref($location),
      currentPage: unref($currentPage),
      pageSize: unref($pageSize)
    }

    const res: Result<Page<EmployeeInfo>> = await http.get('/api/employee/page', params)
    if (res && res.code) {
      list.value = res.data.records
      total.value = res.data.total

      height.value = window.innerHeight - 230
    }
  }

  function chooseCurrentEmployee(emp: EmployeeInfo) {
    currentEmployee.value = {
      id: emp.id,
      name: emp.name,
      job: emp.job,
      managerId: emp.managerId,
      hireDate: emp.hireDate,
      salary: emp.salary,
      commission: emp.commission,
      departmentId: emp.departmentId
    }
  }

  function deleteEmployee(id: number): Promise<Result<null>> {
    const params = {
      id: id
    }
    return http.post(`/api/employee/${id}/delete`, params)
  }


  return { fetchDataForList, chooseCurrentEmployee, deleteCurrentEmployee: deleteEmployee }
}

export const useEmployeePagination = () => {
  const { pagination } = storeToRefs(useEmployeeStore())
  const {} = toRefs(pagination.value)
}

export const useEmployeeDialog = () => {
  const { dialog } = storeToRefs(useEmployeeStore())
  const {
    title,
    visible,
    form,
    $currentEmployee,
    type
  } = toRefs(dialog.value)

  function clearDialogForm() {
    form.value = {} as EmployeeInfo
  }

  function closeDialog() {
    clearDialogForm()
    visible.value = false
  }

  function showDialogForAdd() {
    type.value = DialogType.Add
    title.value = '新增'
    visible.value = true
  }

  function showDialogForUpdate() {
    type.value = DialogType.Update
    title.value = '修改'
    form.value = unref($currentEmployee)
    visible.value = true
  }

  function submitFormForAdd(): Promise<Result<null>> {
    const params = {
      id: unref(form).id,
      name: unref(form).name,
      job: unref(form).job,
      managerId: unref(form).managerId,
      hireDate: unref(form).hireDate,
      salary: unref(form).salary,
      commission: unref(form).commission,
      departmentId: unref(form).departmentId
    }
    return http.post('/api/employee/put', params)
  }

  function submitFormForUpdate(): Promise<Result<null>> {
    const params = {
      id: unref(form).id,
      name: unref(form).name,
      job: unref(form).job,
      managerId: unref(form).managerId,
      hireDate: unref(form).hireDate,
      salary: unref(form).salary,
      commission: unref(form).commission,
      departmentId: unref(form).departmentId
    }

    return http.post(`/api/employee/${params.id}/post`, params)
  }

  return {
    clearDialogForm, closeDialog,
    showDialogForAdd, showDialogForUpdate,
    submitFormForAdd, submitFormForUpdate
  }
}
